package com.jinyi.wwdeng.ui.tab

import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import cn.bingoogolapple.baseadapter.BGAOnItemChildClickListener
import com.jinyi.base.base.BaseActivity
import com.jinyi.base.base.BaseFragment
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.ActTabBinding
import com.jinyi.wwdeng.ui.tab.data.DataFragment
import com.jinyi.wwdeng.ui.tab.home.HomeFragment
import com.jinyi.wwdeng.ui.tab.me.MeFragment
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/17 11:16
 * @Annotation：主页面Tab
 */
class TabAct: BaseActivity() {
    lateinit var binding: ActTabBinding

    private val mTabTitleList = arrayListOf(R.string.tab_home, R.string.tab_data,R.string.tab_me)
    private val mTabNormalResList = arrayListOf(
        R.mipmap.pic_tab_home_normal,
        R.mipmap.pic_tab_data_normal,
        R.mipmap.pic_tab_me_normal,
    )

    private val mTabSelectResList = arrayListOf(
        R.mipmap.pic_tab_home_select,
        R.mipmap.pic_tab_data_select,
        R.mipmap.pic_tab_me_select,
    )
    private var mTabAdapter: TabAdapter? = null

    //定义tab的每个页面
    private val mFragments = ArrayList<BaseFragment>()

    private var mCurrentIndex = 0
    private var mFragmentManager: FragmentManager? = null
    private var mCurrentFragment = Fragment()
    override fun getRootView(): View {
        binding = ActTabBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initView() {
        setBackExit(true)
        setTitleBarVisibility(View.GONE)

        initAddFragment()
        initRecyclerView()
    }

    override fun initListener() {
    }

    override fun initData() {
        //tab
        val tabList = arrayListOf<JSONObject>()
        for (i in 0 until this.mTabTitleList.size) {
            val tabObj = JSONObject()
            tabObj.put("tabId", i)
            tabObj.put("tabTitle", this.mTabTitleList[i])
            tabObj.put("tabNormalRes", this.mTabNormalResList[i])
            tabObj.put("tabSelectRes", this.mTabSelectResList[i])
            tabObj.put("isSelect", false)
            tabList.add(tabObj)
        }
        this.mTabAdapter!!.data = tabList
        setCurrentItemIndex(this.mCurrentIndex)  // 默认选中第一个
    }

    /**
     * 初始化添加Fragment
     */
    private fun initAddFragment() {
        this.mFragments.add(HomeFragment())//首页
        this.mFragments.add(DataFragment())//数据
        this.mFragments.add(MeFragment())//我的
    }

    /**
     * 初始化Tab列表
     */
    private fun initRecyclerView() {
        binding.tabBar.layoutManager = GridLayoutManager(this, 3)
        this.mTabAdapter = TabAdapter(binding.tabBar)
        binding.tabBar.adapter = this.mTabAdapter!!
        binding.tabBar.itemAnimator = DefaultItemAnimator()
        this.mTabAdapter!!.setOnItemChildClickListener(BGAOnItemChildClickListener { _, childView, position ->
            if (childView.id == R.id.itemLayout) {//切换item
                setCurrentItemIndex(position)
            }
        })
    }

    /**
     * 切换tab
     */
    private fun setCurrentItemIndex(index: Int) {
        this.mCurrentIndex = index
        for (tabObj in this.mTabAdapter!!.data) {
            if (tabObj.optInt("tabId") == index) {
                tabObj.put("isSelect", true)
                continue
            }
            tabObj.put("isSelect", false)
        }
        mTabAdapter!!.notifyDataSetChanged()
        showFragment()
    }

    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    private fun showFragment() {
        if (this.mFragmentManager == null) {
            this.mFragmentManager = supportFragmentManager
        }
        val transaction = this.mFragmentManager!!.beginTransaction()
        val fragment: BaseFragment = this.mFragments[this.mCurrentIndex]
        //如果之前没有添加过
        if (!fragment.isAdded) {
            //第三个参数为添加当前的fragment时绑定一个tag
            transaction.hide(this.mCurrentFragment).add(R.id.contentLayout, fragment, "" + this.mCurrentIndex)
        } else {
            transaction.hide(this.mCurrentFragment).show(fragment)
        }
        this.mCurrentFragment = fragment
        if (fragment.isAdded){
//            loadUserInfoAction()
        }
        transaction.commitAllowingStateLoss()
    }
}